package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.mapper.PermissionMapper;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;


    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        //定义当前页的页码和页面大小
        PageHelper.startPage(currentPage,pageSize);

        //条件查询数据
        List<Permission> list = permissionMapper.findByCondition(queryString);

        //将查询的数据给PageInfo进行包装
        PageInfo<Permission> pageInfo = new PageInfo<>(list);

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void add(Permission permission) {
        permissionMapper.add(permission);
    }

    @Override
    public void deleteById(Integer id) {
        //先通过id查询当前检查项有没有被检查组引用
        Integer count =  permissionMapper.findCountByPermissionId(id);
        if (count>0){
            //抛出异常，提示不能删除
            throw new RuntimeException(MessageConstant.CHECKITEM_IS_QUOTED);
        }
        //删除检查项
        permissionMapper.deleteById(id);
    }

    @Override
    public void edit(Permission permission) {
        permissionMapper.edit(permission);
    }
}
